package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemServiceHelper;
import id.ac.amikom.libs.CustomAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListAdapter;
import android.widget.Toast;

public class AdemListUserActivity extends ListActivity {
	
	private JSONArray jsonArray;
	private ListAdapter adapter;
	private String id;
	private String[] uid;
	private String[] name;
	private String[] time;
	private String[] foto;
	private String[] loc;
	
	private class GetDataTask extends AsyncTask<Void, Void, Boolean> {

		ProgressDialog dialog = new ProgressDialog(
				AdemListUserActivity.this);
		boolean result = false;

		@Override
		protected Boolean doInBackground(Void... params) {
	
			AdemServiceHelper srv = new AdemServiceHelper(AdemListUserActivity.this);
			jsonArray = srv.getUserList(id);
			result = true;
			
			return result;

		}

		protected void onPreExecute() {
			this.dialog.setMessage("Loading User Group..");
			this.dialog.show();
		}

		@Override
		protected void onPostExecute(Boolean result) {											
			
			if(result)
				setList();				
			else{
				
				Toast.makeText(AdemListUserActivity.this,"No Internet Connection",
					Toast.LENGTH_LONG).show();
							
				finish();
			}
			
			this.dialog.dismiss();	
		}

	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.adem_title_list);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.adem_title);
	
		Bundle extras = getIntent().getExtras();
		id = extras.getString("_id");
		
		new GetDataTask().execute();		
		
	}	
	
	public void setList() {
		try {

			ArrayList<HashMap<String, Object>> newsList = new ArrayList<HashMap<String, Object>>();
			
			uid  = new String[jsonArray.length()];
			name = new String[jsonArray.length()];
			time = new String[jsonArray.length()];
			foto = new String[jsonArray.length()];
			loc  = new String[jsonArray.length()];
			
			for (int i = 0; i < jsonArray.length(); i++) {
								 								
				JSONObject json = jsonArray.getJSONObject(i);
				HashMap<String, Object> map = new HashMap<String, Object>();
				
				uid[i] = json.getString("uid").toString().trim();
				name[i] = json.getString("fullname").toString().trim();
				time[i] = json.getString("time").toString().trim();		
				foto[i] = json.getString("foto").toString().trim();
				loc[i] = json.getString("location").toString().trim();
												
				map.put("icon", R.drawable.none);				
				map.put("name",name[i]);
				map.put("detail", "updated "+time[i]+"\n"+loc[i]);
				newsList.add(map);

			}

			adapter = new CustomAdapter(this, newsList,
					R.layout.adem_list_img, new String[] { "name", "detail", "icon" },
					new int[] { R.id.list_title, R.id.list_date, R.id.list_image });

			setListAdapter(adapter);
			registerForContextMenu(getListView());

		} catch (JSONException e) {
			e.printStackTrace();

		}
	}
	
	//bontet
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.adem_menu_refresh, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.id_refresh:

			new GetDataTask().execute();

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}
}
